package com.phenix.mvp.base.multitype;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.phenix.mvp.util.TUtil;

import butterknife.ButterKnife;

/**
 * Created by zhouphenix on 2016-7-25.
 */

public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder{
    protected Context mContext;
    protected MultiTypeAdapter mMultiTypeAdapter;

    public BaseViewHolder(View itemView) {
        super(itemView);
        if (!(itemView instanceof ViewGroup)){
            ButterKnife.bind(this, itemView);
            init(itemView);
        }else{
            if (((ViewGroup)itemView).getChildCount()>0 || itemView instanceof RecyclerView || itemView instanceof AdapterView){
                ButterKnife.bind(this, itemView);
                init(itemView);
            }
        }
    }

    public void init(View itemView) {
        mContext = itemView.getContext();
    }


    public Class<?> getTypeClass(){
        Class<?> clazz = TUtil.getTClass(this, 0);
        if (null == clazz) throw new IllegalArgumentException(this.getClass() + " must have generic parameters.");
        return clazz;
    }

    /**
     * 绑定ViewHolder
     * @param holder  可以holder.setIsRecyclable(true);
     * @param t
     */
    public abstract void onBindViewHolder(final RecyclerView.ViewHolder holder, final T t);

    /**
     * 可以做一些判断加载不同的布局，e.g.方向的改变
     * @param recyclerView
     * @param position 位置
     * @return
     */
    public abstract int getLayoutId(final RecyclerView recyclerView, final int position);
}
